package com.weishu.installment.controller;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeWapPayModel;
import com.alipay.api.domain.ExtendParams;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.weishu.installment.config.AlipayConfig;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;

@RestController
@RequestMapping("/pay")
public class AlipayController {

    /**
     *
     * @param response
     * @param out_trade_no 商户订单号，商户网站订单系统中唯一订单号，必填
     * @param subject  订单名称，必填
     * @param total_amount 付款金额，必填
     * @param body 商品描述 可空
     * @param period 分期数
    // * @param timeout_express 超时的时间可空
    // * @param product_code   销售产品码 必填
     */
    @RequestMapping("orders")
    public void pays(HttpServletResponse response, String out_trade_no,String subject,
                     String total_amount,String body,Integer period) {
       // 超时时间 可空
        String timeout_express="2m";
        // 销售产品码 必填
        String product_code="QUICK_WAP_WAY";
        /**********************/
        // SDK 公共请求类，包含公共请求参数，以及封装了签名与验签，开发者无需关注签名与验签
        //调用RSA签名方式
        AlipayClient client = new DefaultAlipayClient(AlipayConfig.URL,
                AlipayConfig.APPID, AlipayConfig.RSA_PRIVATE_KEY,
                AlipayConfig.FORMAT, AlipayConfig.CHARSET,
                AlipayConfig.ALIPAY_PUBLIC_KEY,AlipayConfig.SIGNTYPE);
        AlipayTradeWapPayRequest alipay_request=new AlipayTradeWapPayRequest();
        //分期信息
        ExtendParams extendParams = new ExtendParams();
        extendParams.setHbFqNum(period.toString());
        extendParams.setHbFqSellerPercent("0");
        // 封装请求支付信息
        AlipayTradeWapPayModel model=new AlipayTradeWapPayModel();
        model.setOutTradeNo(out_trade_no);
        model.setSubject(subject);
        model.setTotalAmount(total_amount);
        model.setBody(body);
        model.setTimeoutExpress(timeout_express);
        model.setProductCode(product_code);
        model.setExtendParams(extendParams);
        alipay_request.setBizModel(model);
        // 设置异步通知地址
       // alipay_request.setNotifyUrl(AlipayConfig.notify_url);
        // 设置同步地址
        alipay_request.setReturnUrl(AlipayConfig.return_url);
        // form表单生产
        String form = "";
        try {
            // 调用SDK生成表单
            form = client.pageExecute(alipay_request).getBody();
            response.setContentType("text/html;charset=" + AlipayConfig.CHARSET);
            response.getWriter().write(form);//直接将完整的表单html输出到页面
            response.getWriter().flush();
            response.getWriter().close();
        } catch (AlipayApiException | IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }


    @RequestMapping("ren")
    public void payReturn(HttpServletResponse response,String out_trade_no, BigDecimal total_amount) throws IOException {
        System.out.println(out_trade_no);
        System.out.println(total_amount);
        System.out.println("同步payReturn回调成功");
        response.sendRedirect("http://localhost:80");
    }

//    @RequestMapping("notify")
//    public void payNotify(){
//        System.out.println("异步payNotify回调成功");
//    }

}
